﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using DnDCharacterBuilder.Classes;
using DnDCharacterBuilder.Windows;
using System.Globalization;


namespace DnDCharacterBuilder.UserControls
{
    /// <summary>
    /// Interaction logic for ExtendedListBox.xaml
    /// </summary>
    public partial class ExtendedListBox : UserControl
    {
        public ExtendedListBox()
        {
            InitializeComponent();
        }

        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(string), typeof(ExtendedListBox), new UIPropertyMetadata(""));

        public ObservableCollection<EListItem> Collection
        {
            get { return (ObservableCollection<EListItem>)GetValue(CollectionProperty); }
            set { SetValue(CollectionProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Collection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CollectionProperty =
            DependencyProperty.Register("Collection", typeof(ObservableCollection<EListItem>), typeof(ExtendedListBox), new UIPropertyMetadata(new ObservableCollection<EListItem>() { } ));

        public bool IsAvailable
        {
            get { return (bool)GetValue(IsAvailableProperty); }
            set { SetValue(IsAvailableProperty, value); }
        }
        // Using a DependencyProperty as the backing store for IsAvailable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsAvailableProperty =
            DependencyProperty.Register("IsAvailable", typeof(bool), typeof(ExtendedListBox), new UIPropertyMetadata(false));

        public int PageRef
        {
            get { return (int)GetValue(PageRefProperty); }
            set { SetValue(PageRefProperty, value); }
        }
        // Using a DependencyProperty as the backing store for PageRef.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageRefProperty =
            DependencyProperty.Register("PageRef", typeof(int), typeof(ExtendedListBox), new UIPropertyMetadata(1));




        public ReferenceWindow RefWin
        {
            get { return (ReferenceWindow)GetValue(RefWinProperty); }
            set { SetValue(RefWinProperty, value); }
        }
        // Using a DependencyProperty as the backing store for RefWin.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RefWinProperty =
            DependencyProperty.Register("RefWin", typeof(ReferenceWindow), typeof(ExtendedListBox));





        private void Up_Click(object sender, RoutedEventArgs e)
        {
            if (LB.SelectedIndex > 0)
                Collection.Move(LB.SelectedIndex, LB.SelectedIndex - 1);
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            Collection.Add(new EListItem(name:"Default"));
            if (LB.SelectedIndex - 1 < Collection.Count)
                Collection.Move(Collection.Count - 1, LB.SelectedIndex + 1);
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            if (LB.SelectedIndex >= 0)
                Collection.RemoveAt(LB.SelectedIndex);
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            if (LB.SelectedIndex < Collection.Count - 1)
                Collection.Move(LB.SelectedIndex, LB.SelectedIndex + 1);
        }

        private void Ref_Click(object sender, RoutedEventArgs e)
        {
            if (RefWin == null || !RefWin.IsLoaded)
            {
                RefWin = new ReferenceWindow();
                RefWin.Owner = Window.GetWindow(this);
                RefWin.Show();
            }

            if (!RefWin.IsFocused)
                RefWin.Focus();

            if (LB.SelectedIndex >= 0)
                RefWin.Nav(((EListItem)LB.SelectedItem).PageRef + 1);
            
        }

        private void CB_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ((CheckBox)sender).Visibility = Visibility.Collapsed;
            ((TextBox)((DockPanel)((CheckBox)sender).Parent).Children[2]).Visibility = Visibility.Visible;
            ((TextBox)((DockPanel)((CheckBox)sender).Parent).Children[2]).Focus();
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            ((CheckBox)((DockPanel)((TextBox)sender).Parent).Children[3]).Visibility = Visibility.Visible;
            ((TextBox)sender).Visibility = Visibility.Collapsed;
        }

        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ((Label)sender).Visibility = Visibility.Collapsed;
            ((TextBox)((DockPanel)((Label)sender).Parent).Children[0]).Visibility = Visibility.Visible;
        }

        private void RefBox_LostFocus(object sender, RoutedEventArgs e)
        {
            ((Label)((DockPanel)((TextBox)sender).Parent).Children[1]).Visibility = Visibility.Visible;
            ((TextBox)sender).Visibility = Visibility.Collapsed;
        }



    }
  
    [ValueConversion(typeof(int), typeof(string))]
    public class PageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int val;
            try
            {
                val = (int)value;
            }
            catch
            {
                val = 0;
            }
            return "(pg. " + value.ToString() + ")";
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
